import '../../util/_base_export.dart';
import '../../util/_view_export.dart';

import 'package:flutter_form_builder/flutter_form_builder.dart';

import '../../util/app_util.dart';
import '../../blocs/code.dart';

class AccountRedeemPage extends StatefulWidget {
  // final String type;
  const AccountRedeemPage({Key? key}) : super(key: key);
  @override
  _AccountRedeemPageState createState() => _AccountRedeemPageState();
}

class _AccountRedeemPageState extends State<AccountRedeemPage> {
  @override
  initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((callback) {
      Provider.of<CodeBloc>(context, listen: false).init();
    });
  }

  GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();

  @override
  Widget build(BuildContext context) {
    ValueChanged _onChangedDate = (val) {};
    // print(widget.type);
    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            child: Icon(
              Icons.chevron_left,
              color: AppColor.blackMain,
              size: 32,
            ),
          ),
        ),
        title: Text('兑换', style: TextStyle(color: AppColor.blackMain)),
      ),
      body: SingleChildScrollView(
        child: Container(
          child: Consumer<CodeBloc>(
            builder: (context, _code, child) => Builder(
              builder: (context) {
                return Column(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: <Widget>[
                    Container(
                      height: 40.h,
                      width: 338.w,
                      margin: EdgeInsets.symmetric(
                        horizontal: 25.w,
                        vertical: 10.h,
                      ),
                      child: FormBuilder(
                        // context,
                        key: _fbKey,
                        autovalidateMode: AutovalidateMode.always,
                        initialValue: {
                          "code": '',
                        },
                        enabled: false,
                        child: Container(
                          padding: EdgeInsets.only(
                            left: 10.w,
                          ),
                          child: FormBuilderTextField(
                            name: "code",
                            decoration: InputDecoration(
                              labelText: "输入兑换码",
                              contentPadding: EdgeInsets.all(10.0),
                              border: OutlineInputBorder(
                                borderRadius: BorderRadius.all(Radius.circular(18.0)),
                              ), //former builder⾥的弧度要⽐外边⼩
                              suffixIcon: Container(
                                height: 26.h,
                                width: 70.w,
                                margin: EdgeInsets.symmetric(vertical: 5.h, horizontal: 5.w),
                                decoration: BoxDecoration(
                                  color: AppColor.yellowMain,
                                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
                                ),
                                child: TextButton(
                                  onPressed: () async {
                                    _fbKey.currentState?.save();

                                    if ((_fbKey.currentState?.value['code'] ?? '').isNotEmpty) {
                                      // String _type =
                                      //     ((widget.type ?? '').isEmpty) ? '' : widget.type;
                                      // print(_type);
                                      var lv_result =
                                          await _code.codeQuery(_fbKey.currentState?.value['code']);
                                    } else {
                                      print('sss');
                                    }
                                  },
                                  child: Text(
                                    '搜索',
                                    style: TextStyle(color: AppColor.blackMain, fontSize: 13.sp),
                                  ),
                                  style: ButtonStyle(
                                    overlayColor: MaterialStateColor.resolveWith((states) => Color(0x55FF8A80)),
                                    //shadowColor: Color(0x99FF8A80),
                                  ),
                                ),
                              ),
                              prefixIcon: Icon(Icons.search, color: AppColor.yellowMain),
                            ),
                            // onChanged: _onChanged,
                            // valueTransformer: (text) => num.tryParse(text),
                            //validators: [],
                            keyboardType: TextInputType.text,
                          ),
                        ),
                      ),
                    ),

                    // FormBuilder(
                    //   key: _fbKey,
                    //   autovalidateMode: AutovalidateMode.always,
                    //   initialValue: {
                    //     "code": '',
                    //   },
                    //   readOnly: false,
                    //   child: Column(
                    //     children: <Widget>[
                    //       Container(
                    //         width: ScreenUtil().screenWidth.toDouble(),
                    //         // decoration: AppDecoration.buttonTwo, //
                    //         margin: EdgeInsets.symmetric(horizontal: 50.w),
                    //         padding: EdgeInsets.symmetric(
                    //           horizontal: 10.w,
                    //         ),
                    //         child: ButtonTheme(
                    //           alignedDropdown: true,
                    //           child: FormBuilderTextField(
                    //             attribute: "code",
                    //             decoration: InputDecoration(
                    //               labelText: '输入兑换码', //
                    //             ),
                    //             //Text(AppText.text['login_region_tooltip']),
                    //             maxLines: 1,
                    //             minLines: 1,
                    //             validators: [],
                    //             keyboardType: TextInputType.text,
                    //             onChanged: _onChangedDate,
                    //           ),
                    //         ),
                    //       ),
                    //       SizedBox(height: 15.h),
                    //       FlatButton(
                    //         child: Container(
                    //           width: 100.w,
                    //           height: 30.h,
                    //           decoration: AppDecoration.buttonOne,
                    //           child: Center(
                    //             child: Text('查找'),
                    //           ),
                    //         ),
                    //         onPressed: () async {
                    //           _fbKey.currentState.save();

                    //           if ((_fbKey.currentState.value['code'] ?? '').isNotEmpty) {
                    //             var lv_result =
                    //                 await _code.codeQuery(_fbKey.currentState.value['code']);
                    //           } else {
                    //             print('sss');
                    //           }
                    //         },
                    //       ),
                    //     ],
                    //   ),
                    // ),
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 16.h),
                      child: (_code.coupon.isEmpty)
                          ? Container()
                          : Card(
                              child: Container(
                                margin: EdgeInsets.symmetric(horizontal: 10.w),
                                width: ScreenUtil().screenWidth.toDouble(),
                                child: Row(
                                  children: <Widget>[
                                    Container(
                                      width: 128.w,
                                      height: 128.h,
                                      child: CachedNetworkImage(
                                        imageUrl: _code.coupon['image_url'],
                                        fit: BoxFit.cover,
                                      ),
                                    ),
                                    Container(
                                      width: 188.w,
                                      margin: EdgeInsets.symmetric(
                                        horizontal: 15.w,
                                      ),
                                      child: Column(
                                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: <Widget>[
                                          Text(
                                            _code.coupon['name'],
                                            overflow: TextOverflow.ellipsis,
                                            style: TextStyle(
                                              color: AppColor.blackMain,
                                              fontSize: 12.sp,
                                            ),
                                            maxLines: 2,
                                            softWrap: true,
                                          ),
                                          TextButton(
                                            child: Text(
                                              '领取',
                                              style: TextStyle(
                                                color: Colors.orange,
                                                fontSize: 14.sp,
                                              ),
                                            ),
                                            onPressed: () async {
                                              // String _type =
                                              //     ((widget.type ?? '').isEmpty) ? '' : widget.type;
                                              var lv_result =
                                                  await _code.codeRedeem(_code.coupon['code']);
                                              if (lv_result) {
                                                AppPopup.popupToast('S', '成功领取');
                                                Navigator.pop(context);
                                              }
                                            },
                                          ),
                                        ],
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ),
                    ),
                  ],
                );
              },
            ),
          ),
        ),
      ),
    );
  }
}
